package com.cucn.ingmall.product.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cucn.common.utils.PageUtils;
import com.cucn.common.utils.R;
import com.cucn.ingmall.product.entity.BrandEntity;
import com.cucn.ingmall.product.entity.CategoryBrandRelationEntity;
import com.cucn.ingmall.product.service.CategoryBrandRelationService;
import com.cucn.ingmall.product.vo.BrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author kouchaojie
 * @email kou1688@foxmail.com
 * @date 2021-11-17 17:07:49
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取当前品牌关联的所有分类列表
     *
     * @param brandId 品牌id
     * @return 统一结果返回
     */
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam Long brandId) {
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("brand_id", brandId));

        return R.ok().put("data", data);
    }

    /**
     * 获取分类关联的品牌
     * 1.controller处理请求，接收和校验数据
     * 2.service接受controller传来的数据，进行业务处理
     * 3.controller接收service处理完的数据，封装成页面指定的vo
     *
     * @param catId 分类id
     * @return 统一结果返回-分类关联的品牌集合
     */
    @GetMapping("/brands/list")
    public R categoryRelationBrandList(@RequestParam(value = "catId", required = true) Long catId) {
        //根据分类id查询关联的品牌
        List<BrandEntity> data = categoryBrandRelationService.getBrandsByCatId(catId);
        List<BrandVO> collect = data.stream().map((item) -> {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandId(item.getBrandId());
            brandVO.setBrandName(item.getName());
            return brandVO;
        }).collect(Collectors.toList());

        return R.ok().put("data", collect);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 新增品牌与分类关联关系
     *
     * @param categoryBrandRelation 前端传来的关联关系:brandId,catelogId
     * @return 统一结果返回
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
